package com.customer.crm.product.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.customer.crm.product.pojo.ProductCategoryPojo;
import com.customer.crm.product.pojo.ProductPojo;
import com.customer.crm.product.service.impl.ProductCategoryServiceImpl;
import com.customer.crm.product.service.impl.ProductServiceImpl;
import com.customer.crm.system.pojo.SystemUser;
import com.customer.crm.system.service.impl.SystemUserServiceImpl;
import com.customer.crm.util.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class ProductController {
    @Resource
    private ProductServiceImpl productService;
    @Resource
    private ProductCategoryServiceImpl productCategoryService;
    @Resource
    private SystemUserServiceImpl userService;

    //通过点击产品path路径找到list_product这个页面
    @RequestMapping("/system/product/ProductAction_list.action")
    public String toListProduct(ModelMap modelMap) {

        return "/views/manage/product/list_product";
    }

    //加载用户数据
    @ResponseBody
    @RequestMapping("/ProductFindByPage")
    public ResultData findProduct(long page, long rows, ModelMap modelMap) {
        Page<ProductPojo> productPage = new Page<>(page, rows);
        Page<ProductPojo> page1 = productService.page(productPage, null);
        List<ProductPojo> records = page1.getRecords();
        for (ProductPojo li:records
        ) {
            String creatorUserId = li.getCreatorUserId();
            SystemUser userPojo = userService.getById(creatorUserId);
            String username = userPojo.getUserName();
            li.setCreatorUserId(username);
        }
        modelMap.addAttribute("totalRows", page1.getTotal());
        modelMap.addAttribute("result", page1.getRecords());
        ResultData resultData = ResultData.buildFailureResult("data", modelMap);
        return resultData;
    }


    //添加
    @RequestMapping("/toProductInsert")
    public String toProductInsert(ModelMap modelMap) {
        List<ProductCategoryPojo> categoryPojoList = productCategoryService.list();
        modelMap.addAttribute("categoryPojoList", categoryPojoList);
        List<SystemUser> userPojoList = userService.list();
        modelMap.addAttribute("userPojoList", userPojoList);
        return "/views/manage/product/insert_product";
    }

    //添加保存
    @ResponseBody
    @RequestMapping("/insertProduct")
    public ModelMap insertProduct(ProductPojo productPojo) {
        ModelMap modelMap = new ModelMap();
        int  id=0;
        id  = (int) (Math.random() * 1000);
        ProductPojo byId = productService.getById(id);
        if (byId == null) {
            productPojo.setId(id);
        } else {
            int a = (int) (Math.random() * 10);
            id += a;
            productPojo.setId(id);
        }

        boolean b = productService.save(productPojo);
        if (b == true) {
            modelMap.addAttribute("message", "添加成功");
        } else {
            modelMap.addAttribute("message", "添加失败");
        }
        return modelMap;
    }


    //修改页面
    @RequestMapping("/toUpdateProduct")
    public String toUpdateProduct(@RequestParam("id") String id, ModelMap modelMap) {
        ProductPojo byId = productService.getById(id);
        modelMap.addAttribute("productPojo", byId);
        List<SystemUser> userPojoList = userService.list();
        modelMap.addAttribute("userPojoList", userPojoList);
        List<ProductCategoryPojo> categoryPojoList = productCategoryService.list();
        modelMap.addAttribute("categoryPojoList", categoryPojoList);
        return "/views/manage/product/update_product";

    }

    //为保存按钮修盖事件
    @ResponseBody
    @RequestMapping("/updateProduct")
    public ModelMap updateProduct(ProductPojo productPojo) {
        ModelMap modelMap = new ModelMap();
        boolean b = productService.updateById(productPojo);
        if (b == true) {
            modelMap.addAttribute("message", "修改成功");
        } else {
            modelMap.addAttribute("message", "修改失败");
        }
        return modelMap;
    }


    //删除按钮事件处理函数
    @ResponseBody
    @RequestMapping("/product_remove")
    public ModelMap delProduct(@RequestParam("ids") String[] ids) {
        ModelMap modelMap = new ModelMap();
        if (ids != null && ids.length > 0) {
            for (String id : ids
            ) {
                boolean b = productService.removeById(id);
                if (b == true) {
                    modelMap.addAttribute("message", "删除成功");
                } else {
                    modelMap.addAttribute("message", "删除失败");
                }
            }
        }
        return modelMap;
    }


    @ResponseBody
    @RequestMapping("/productLike")
    public ResultData productLike(@RequestParam("product.name")String name,@RequestParam("product.suggestedPrice")String suggestedPrice,ModelMap modelMap,long page,long rows){
        Page<ProductPojo> productPage = new Page(page,rows);
        ResultData resultData=null;
        if (name !=null && suggestedPrice !=null){
            Page<ProductPojo> page1 = productService.page(productPage, new QueryWrapper<ProductPojo>().like("name", name).like("suggested_price", suggestedPrice));
            modelMap.addAttribute("totalRows",page1.getTotal());
            modelMap.addAttribute("result",page1.getRecords());
             resultData = ResultData.buildFailureResult("data", modelMap);
        }else if (name!=null && suggestedPrice ==null){
            Page<ProductPojo> page1 = productService.page(productPage, new QueryWrapper<ProductPojo>().like("name", name));
            modelMap.addAttribute("totalRows",page1.getTotal());
            modelMap.addAttribute("result",page1.getRecords());
            resultData = ResultData.buildFailureResult("data", modelMap);
         }else if (name==null && suggestedPrice !=null ){
            Page<ProductPojo> page1 = productService.page(productPage, new QueryWrapper<ProductPojo>().like("suggested_price", suggestedPrice));
            modelMap.addAttribute("totalRows",page1.getTotal());
            modelMap.addAttribute("result",page1.getRecords());
            resultData = ResultData.buildFailureResult("data", modelMap);
         }else if (name==null && suggestedPrice ==null){
            Page<ProductPojo> page1 = productService.page(productPage,null);
            modelMap.addAttribute("totalRows",page1.getTotal());
            modelMap.addAttribute("result",page1.getRecords());
            resultData = ResultData.buildFailureResult("data", modelMap);
        }

        return resultData;
    }

    @RequestMapping("/ProductAction_modify")
    public String productAction_modify(@RequestParam("taskVo.id")String id,ModelMap modelMap){
        ProductPojo byId = productService.getById(id);
        modelMap.addAttribute("productPojo", byId);
        List<SystemUser> userPojoList = userService.list();
        modelMap.addAttribute("userPojoList", userPojoList);
        List<ProductCategoryPojo> categoryPojoList = productCategoryService.list();
        modelMap.addAttribute("categoryPojoList", categoryPojoList);
        return "/views/manage/product/update_product";
    }

    @RequestMapping("/ProductAction_see")
    public String productAction_see(@RequestParam("taskVo.id")String id,ModelMap modelMap){
        ProductPojo byId = productService.getById(id);
        modelMap.addAttribute("productPojo", byId);
        Integer categoryId = byId.getCategoryId();
        ProductCategoryPojo categoryPojo = productCategoryService.getById(categoryId);
        modelMap.addAttribute("categoryPojo", categoryPojo);
        String creatorUserId = byId.getCreatorUserId();
        SystemUser userPojo = userService.getById(creatorUserId);
        modelMap.addAttribute("userPojo", userPojo);
        return "/views/manage/product/see_product";
    }
}
